package com.quanxiaoha.ai.robot.controller;

import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesis;
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisParam;
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisResult;
import com.alibaba.dashscope.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author Caius
 * @description
 * @since Created in 2025-09-14
 */
@RestController
@RequestMapping("/v12/ai")
@Slf4j
public class Text2VideoController {
    @Value("${spring.ai.openai.api-key}")
    private String apiKey;

    @GetMapping("text2video")
    public String text2video(@RequestParam(value = "prompt") String prompt) {
        HashMap<String, Object> parameter = new HashMap<>();
        parameter.put("prompt_extend", true);

        String imgUrl = "file:///" + "D:/xiaojiejie.jpg";

        // 构建调用大模型所需参数
        VideoSynthesisParam param = VideoSynthesisParam.builder()
                .apiKey(apiKey)
                .model("wan2.2-i2v-plus")
                .prompt(prompt)
                .imgUrl(imgUrl)
                .parameters(parameter)
                .build();

        log.info("## 正在生成中, 请稍等...");

        VideoSynthesis vs = new VideoSynthesis();
        VideoSynthesisResult result = null;

        try {
            result = vs.call(param);
        } catch (Exception e) {
            log.error("", e);
        }
        return JsonUtils.toJson(result);
    }

}
